//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BlackAtmVolCurve.h"
using namespace Cephei::QL::Experimental::Volatility;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/VolatilityTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::CBlackAtmVolCurve (boost::shared_ptr<QuantLib::BlackAtmVolCurve>& childNative, Object^ owner) : CVolatilityTermStructure(CBlackAtmVolCurve::typeid)
{
#ifdef HANDLE
	_phBlackAtmVolCurve = NULL;
#endif
	_ppBlackAtmVolCurve = &childNative;
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppBlackAtmVolCurve));
}
Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::CBlackAtmVolCurve (QuantLib::BlackAtmVolCurve& childNative, Object^ owner) : CVolatilityTermStructure(CBlackAtmVolCurve::typeid)
{
#ifdef HANDLE
	_phBlackAtmVolCurve = NULL;
#endif
	_ppBlackAtmVolCurve = new boost::shared_ptr<QuantLib::BlackAtmVolCurve> (&childNative);
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppBlackAtmVolCurve));
    _BlackAtmVolCurveOwner = owner;
    _VolatilityTermStructureOwner = owner;
}

Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::CBlackAtmVolCurve (CBlackAtmVolCurve^ copy) : CVolatilityTermStructure(CBlackAtmVolCurve::typeid)
{
#ifdef HANDLE
	_phBlackAtmVolCurve = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackAtmVolCurve = new boost::shared_ptr<QuantLib::BlackAtmVolCurve> (copy->GetShared());
        _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppBlackAtmVolCurve));
    }
}
Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::CBlackAtmVolCurve (System::Type^ t) : CVolatilityTermStructure(CBlackAtmVolCurve::typeid)
{
#ifdef HANDLE
	_phBlackAtmVolCurve = NULL;
#endif
	if (!t->IsSubclassOf(CBlackAtmVolCurve::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::CBlackAtmVolCurve (QuantLib::Handle<QuantLib::BlackAtmVolCurve>& childNative, Object^ owner)  : CVolatilityTermStructure(CBlackAtmVolCurve::typeid)
{
	_phBlackAtmVolCurve = &childNative;
	_ppBlackAtmVolCurve = &static_cast<boost::shared_ptr<QuantLib::BlackAtmVolCurve>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppBlackAtmVolCurve));
    _BlackAtmVolCurveOwner = owner;
}
Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::CBlackAtmVolCurve (QuantLib::Handle<QuantLib::BlackAtmVolCurve> childNative)  : CVolatilityTermStructure(CBlackAtmVolCurve::typeid)
{
	_phBlackAtmVolCurve = &childNative;
	_ppBlackAtmVolCurve = &static_cast<boost::shared_ptr<QuantLib::BlackAtmVolCurve>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppBlackAtmVolCurve));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::CBlackAtmVolCurve (QuantLib::BlackAtmVolCurve childNative)  : CVolatilityTermStructure(CBlackAtmVolCurve::typeid)
{
#ifdef HANDLE
	_phBlackAtmVolCurve = NULL;
#endif
	_ppBlackAtmVolCurve = new boost::shared_ptr<QuantLib::BlackAtmVolCurve> (new QuantLib::BlackAtmVolCurve (childNative));
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppBlackAtmVolCurve));
}
#endif

Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::~CBlackAtmVolCurve ()
{
    if (_ppBlackAtmVolCurve != NULL)
    {
	    delete _ppBlackAtmVolCurve;
        _ppBlackAtmVolCurve = NULL;
    }
}
Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::!CBlackAtmVolCurve ()
{
    if (_ppBlackAtmVolCurve != NULL)
    {
	    delete _ppBlackAtmVolCurve;
    }
}
QuantLib::BlackAtmVolCurve& Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::GetReference ()
{
    if (_ppBlackAtmVolCurve == NULL) throw gcnew NativeNullException ();
	return **_ppBlackAtmVolCurve;
}
boost::shared_ptr<QuantLib::BlackAtmVolCurve>& Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::GetShared ()
{
    if (_ppBlackAtmVolCurve == NULL) throw gcnew NativeNullException ();
	return *_ppBlackAtmVolCurve;
}
QuantLib::BlackAtmVolCurve* Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::GetPointer ()
{
    if (_ppBlackAtmVolCurve == NULL) throw gcnew NativeNullException ();
	return &**_ppBlackAtmVolCurve;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackAtmVolCurve>& Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::GetHandle ()
{
	if (_phBlackAtmVolCurve == NULL)
	{
		_phBlackAtmVolCurve = new Handle<QuantLib::BlackAtmVolCurve> (*_ppBlackAtmVolCurve);
	}
	return *_phBlackAtmVolCurve;
}
#endif
bool Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::HasNative () 
{
	return (_ppBlackAtmVolCurve != NULL);
}

Double Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::AtmVariance (Double maturity, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackAtmVolCurve)->atmVariance ( _maturity,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::AtmVariance (DateTime maturity, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Date _maturity = (QuantLib::Date)ValueHelper::Convert (maturity);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackAtmVolCurve)->atmVariance ( _maturity,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::AtmVariance (Cephei::QL::Times::IPeriod^ optionTenor, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CoptionTenor;
    try
    {
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackAtmVolCurve)->atmVariance ( _optionTenor,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
    }
}
Double Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::AtmVol (Double maturity, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppBlackAtmVolCurve)->atmVol ( _maturity,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::AtmVol (DateTime maturity, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Date _maturity = (QuantLib::Date)ValueHelper::Convert (maturity);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppBlackAtmVolCurve)->atmVol ( _maturity,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Volatility::CBlackAtmVolCurve::AtmVol (Cephei::QL::Times::IPeriod^ optionTenor, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CoptionTenor;
    try
    {
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppBlackAtmVolCurve)->atmVol ( _optionTenor,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

